package com.ge.med.cchelper.popup.job;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.ge.med.cchelper.bean.Response;
import com.ge.med.cchelper.form.CompareForm;
import com.ge.med.cchelper.util.LogUtil;
import com.ge.med.cchelper.util.SSLhelper;
import com.ge.med.cchelper.util.StringHelper;

public class CompareJob extends BaseJob{

	private IFile selectedIFile;

	
	public CompareJob(String message,IFile selectedIFile) {
		super(message);
		this.selectedIFile = selectedIFile;	
	}

	
	protected IStatus start(IProgressMonitor arg0) {
		String noUse = selectedIFile.getFullPath().segment(1);
		String allPath = selectedIFile.getFullPath().toString();
		String realTempPath = allPath.substring(allPath
				.indexOf(noUse) + noUse.length());
		String fileName = selectedIFile.getFullPath()
				.lastSegment();

		String fullPath = selectedIFile.getLocation()
				.toOSString();
		String vobsPath = csession.getVobRoot();
		String remotePath = vobsPath + realTempPath;

		LogUtil.out("compare " + fileName + "...");
		
		SSLhelper sslHelper = new SSLhelper(csession);
		
		Response res = sslHelper.executeCT(sslHelper.getCleartool()+" ls "+remotePath);
		int flag = res.getStatus();
		if (flag == 0) {
			String verinfo = res.getContent().toString();
			String realPwd = StringHelper.matchVobPath(verinfo);
			String[] vers =realPwd.split(" ");
			Display.getDefault().asyncExec(new VersionRunnabel(vers[0],sourceTempDir,fullPath,fileName));  
		}else{
			return Status.CANCEL_STATUS;
		}
		
		return Status.OK_STATUS;
	}
	
	 static class VersionRunnabel implements Runnable {
		String verinfo;
		String sourceTempDir;
		String localFilePath;
		String fileName;

		public VersionRunnabel(String verinfo,String sourceTempDir,String localFilePath,String fileName) {
			this.verinfo = verinfo;
			this.sourceTempDir = sourceTempDir;
			this.localFilePath = localFilePath;
			this.fileName=fileName;
		}

		
		public void run() {
			CompareForm inst = new CompareForm(new Shell(Display.getDefault()), SWT.NULL);
			inst.initParameter(verinfo,sourceTempDir,localFilePath,fileName);
			inst.open();
		}
	}


}
